﻿Imports System.Activities
Imports System.Windows.Data
Imports System.Activities.Expressions
Imports Microsoft.VisualBasic.Activities
Imports System.Activities.Presentation.Model

''' <summary>InArgument と String 値を変換する</summary>
Public Class InArgumentToStringConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim resultValue As String = Nothing

        If TypeOf value Is InArgument(Of String) Then
            Dim exp = DirectCast(value, InArgument(Of String)).Expression
            If TypeOf exp Is Literal(Of String) Then
                resultValue = DirectCast(exp, Literal(Of String)).Value
            ElseIf TypeOf exp Is VisualBasicValue(Of String) Then
                resultValue = DirectCast(exp, VisualBasicValue(Of String)).ExpressionText
            End If
        ElseIf TypeOf value Is ModelItem Then
            Dim exp = TryCast(DirectCast(value, ModelItem).GetCurrentValue, InArgument(Of String))
            If TypeOf exp.Expression Is Literal(Of String) Then
                resultValue = DirectCast(exp.Expression, Literal(Of String)).Value
            ElseIf TypeOf exp.Expression Is VisualBasicValue(Of String) Then
                resultValue = DirectCast(exp.Expression, VisualBasicValue(Of String)).ExpressionText
            End If
        End If
        Return resultValue
    End Function

    Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Dim resultValue As InArgument(Of String) = Nothing
        If TypeOf value Is String Then
            resultValue = New InArgument(Of String)(
                                     New Literal(Of String)(
                                         CType(value, String)))
        End If
        Return resultValue
    End Function

End Class
